<?php
/**
 * Created by PhpStorm.
 * User: 86158
 * Date: 2021/6/26
 * Time: 23:20
 */

namespace App\Admin\Controllers\goods;


use App\Common\statement\CommonStatement;
use App\Models\goods\Goods;
use App\Models\goods\GoodsOrder;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ExchangeController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '商品兑换记录';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */

    public function grid()
    {
        $grid = new Grid(new GoodsOrder());
        $grid->model()->orderBy('id');
        $grid->disableExport();
        $grid->disableCreateButton();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
        });
        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->column(1/3, function ($filter) {
                $filter->equal( 'status',__('Status'))->select(CommonStatement::exchang_status);
            });

            $filter->column(1/3, function ($filter) {
                $filter->between('created_at',__('Created at'))->datetime();
            });

        });
        $grid->column('id',__('ID'));
        $grid->column('goods.name',__('Goods Name'));
        $grid->column('user.name',__('User Name'));
        $grid->column('status',__('Status'))->editable('select',CommonStatement::exchang_status);
        $grid->column('created_at',__('Created at'));
        return $grid;
    }

    public function detail($id)
    {
        $show = new Show(GoodsOrder::findOrFail($id));
        $show->field('status');
        return $show;
    }
    
    public function form()
    {
        $model = new GoodsOrder();

        $form = new Form($model);
        $form->text('user.name',__('User Name'))->readonly();
        $form->text('goods.name',__('Goods Name'))->readonly();
        $form->text('integral',__('Money'))->readonly();
        $form->select('status',__('Status'))->options(CommonStatement::exchang_status);
        $form->text('created_at',__('兑换时间'))->readonly();
        $form->saving(function (Form $form){


            if($form->model()->stauts == 1){
                $money = $form->model()->user->money;
                $user = User::where(['id'=>$form->model()->user_id])->update(['money'=>bcadd($money,$form->model()->integral,2)]);
                $stock = Goods::where('id',$form->model()->goods_id)->increment('stock',1);
            }

            if($form->model()->status == 1 && $form->status != 1){
                $money = $form->model()->user->money;
                $user = User::where(['id'=>$form->model()->user_id])->update(['money'=>bcsub($money,$form->model()->integral,2)]);
                $stock = Goods::where('id',$form->model()->goods_id)->decrement('stock',1);
            }
        });
        return $form;
    }
}